package org.example.config;

import lombok.extern.slf4j.Slf4j;
import org.example.exception.ServiceException;
import org.example.utils.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常
 */
@ControllerAdvice   //AOP - 面向切面编程
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(ServiceException.class)   //处理ServiceException这类异常
    @ResponseBody   //以json的格式返回数据
    public R errorHandle(ServiceException e){
        log.error(e.getMessage(),e);
        return R.fail(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R errorHandle(Exception e){
        log.error(e.getMessage(),e);
        return R.fail("系统错误，清联系管理员处理");
    }
}
